import { useResponseSuccess } from '~/utils/response';

export default eventHandler(async (event) => {
  const query = getQuery(event);
  const { keyword, resourceType, status, page = 1, pageSize = 10 } = query;

  // 模拟库存数据
  const inventory = [
    {
      id: '1',
      name: '豪华套房',
      code: 'ROOM_001',
      resourceType: 'PHYSICAL',
      quantity: 5,
      minQuantity: 2,
      maxQuantity: 10,
      usageRate: 85,
      status: 'NORMAL',
      updateTime: '2024-01-15 10:30:00',
    },
    {
      id: '2',
      name: '温泉池',
      code: 'POOL_001',
      resourceType: 'PHYSICAL',
      quantity: 1,
      minQuantity: 2,
      maxQuantity: 5,
      usageRate: 95,
      status: 'LOW',
      updateTime: '2024-01-15 09:15:00',
    },
    {
      id: '3',
      name: 'VIP包间',
      code: 'VIP_001',
      resourceType: 'PHYSICAL',
      quantity: 0,
      minQuantity: 1,
      maxQuantity: 3,
      usageRate: 100,
      status: 'OUT',
      updateTime: '2024-01-15 08:45:00',
    },
    {
      id: '4',
      name: '数字会议室',
      code: 'DIGITAL_001',
      resourceType: 'DIGITAL',
      quantity: 8,
      minQuantity: 5,
      maxQuantity: 10,
      usageRate: 80,
      status: 'NORMAL',
      updateTime: '2024-01-15 11:20:00',
    },
    {
      id: '5',
      name: '按摩服务',
      code: 'SERVICE_001',
      resourceType: 'SERVICE',
      quantity: 12,
      minQuantity: 10,
      maxQuantity: 15,
      usageRate: 75,
      status: 'NORMAL',
      updateTime: '2024-01-15 10:00:00',
    },
  ];

  // 过滤数据
  let filteredData = inventory;

  if (keyword) {
    filteredData = filteredData.filter(item =>
      item.name.includes(keyword as string) ||
      item.code.includes(keyword as string)
    );
  }

  if (resourceType) {
    filteredData = filteredData.filter(item =>
      item.resourceType === resourceType
    );
  }

  if (status) {
    filteredData = filteredData.filter(item =>
      item.status === status
    );
  }

  // 分页
  const start = (Number(page) - 1) * Number(pageSize);
  const end = start + Number(pageSize);
  const items = filteredData.slice(start, end);

  return useResponseSuccess({
    items,
    total: filteredData.length,
    page: Number(page),
    pageSize: Number(pageSize),
  });
});
